package cn.edu.stu.syllabus.repository.oa.local;

import android.os.Parcel;
import android.os.Parcelable;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.Transient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.edu.stu.syllabus.switcher.DayNightSwitcher;
import cn.edu.stu.syllabus.utils.AssetsUtils;

import org.greenrobot.greendao.annotation.Generated;

/**
 * Created by daidaijie on 2017/3/14.
 */

@Entity
public class OaInfoEntry implements Parcelable {

    @Id(autoincrement = true)
    private Long mLocalId;

    @Index(unique = true)
    private Long mRemoteId;

    private String mTitle;

    private String mSubCompanyName;

    private int mSubCompanyId;

    private String mDocContent;

    private String mDate;

    private String mTime;

    private int mAccessFileCount;

    @Transient
    private boolean isRead;

    public String getHtmlContentAfterFormatter() {
        String oaContent = "";
        String label = "!@#$%^&*";

        oaContent = mDocContent;
        int index = oaContent.indexOf(label) + label.length();
        oaContent = oaContent.substring(index);

        Document contentDoc = Jsoup.parse(oaContent);

        Elements elements = contentDoc.getAllElements();
        for (Element element : elements) {
            boolean isH = false;
            if (element.nodeName().matches("^h\\d+$")) {
                isH = true;
            }
            String[] styles = element.attr("style").split(";");
            StringBuffer sb = new StringBuffer();
            for (String style : styles) {
                if (style.contains("text-align") || style.contains("margin")
                        && !style.contains("margin-")) {
                    sb.append(style);
                    sb.append(";");
                } else if (style.contains("text-indent")) {
                    sb.append("text-indent: 2pm;");
                }
            }
            if (isH) {
                sb.append("font-size:18px;");
            }

            element.attr("style", sb.toString());
        }

        // 去掉那些图片，根本不显示
        Elements imgs = contentDoc.select("img");
        for (Element img : imgs) {
            img.remove();
        }

        // 适应表格大小
        Elements tables = contentDoc.getElementsByTag("table");
        for (Element table : tables) {
            table.attr("width", "100%");
            table.attr("style", "width: 100%;");
            Elements trs = table.select("tr");
            for (Element tr : trs) {
                Elements tds = tr.select("td");
                double witdh = 100.0 / tds.size();
                for (Element td : tds) {
                    String colspan = td.attr("colspan");
                    if (colspan.trim().isEmpty()) {
                        td.attr("style", "width:" + witdh + "%");
                    } else {
                        td.attr("style", "width:" + witdh * Integer.parseInt(colspan) + "%");
                    }
                    td.removeAttr("nowrap");
                }
            }
        }

        // 去掉font定义的坑爹属性
        Elements fonts = contentDoc.getElementsByTag("font");
        for (Element font : fonts) {
            font.removeAttr("size");
            font.removeAttr("color");
            font.removeAttr("face");
        }

        String linkColor = "#7986cb";
        Elements links = contentDoc.getElementsByTag("a");
        for (Element link : links) {
            link.attr("style", "color:" + linkColor);
        }

        Document doc = Jsoup.parse(AssetsUtils.getStringFromPath("index.html"));
        Element div = doc.select("div#div_doc").first();
        div.append(contentDoc.toString());

        // 设置字体颜色
        Element body = doc.getElementsByTag("body").get(0);
        String bgColorValue = DayNightSwitcher.getInstance().isNightMode() ? "#303030" : "#FAFAFA";
        String textColorValue = DayNightSwitcher.getInstance().isNightMode() ? "#A8A8A8" : "#212121";

        StringBuffer bodyStyle = new StringBuffer();
        bodyStyle.append("color:");
        bodyStyle.append(textColorValue);
        bodyStyle.append(";");
        bodyStyle.append("background:");
        bodyStyle.append(bgColorValue);
        bodyStyle.append(";");
        bodyStyle.append("letter-spacing:1px");
        bodyStyle.append(";");
        bodyStyle.append("line-height:150%");

        body.attr("style", bodyStyle.toString());

        return doc.toString();
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(this.mRemoteId);
        dest.writeString(this.mTitle);
        dest.writeString(this.mSubCompanyName);
        dest.writeInt(this.mSubCompanyId);
        dest.writeString(this.mDocContent);
        dest.writeString(this.mDate);
        dest.writeString(this.mTime);
        dest.writeInt(this.mAccessFileCount);
    }


    public Long getMRemoteId() {
        return this.mRemoteId;
    }


    public void setMRemoteId(Long mRemoteId) {
        this.mRemoteId = mRemoteId;
    }


    public String getMTitle() {
        return this.mTitle;
    }


    public void setMTitle(String mTitle) {
        this.mTitle = mTitle;
    }


    public String getMSubCompanyName() {
        return this.mSubCompanyName;
    }


    public void setMSubCompanyName(String mSubCompanyName) {
        this.mSubCompanyName = mSubCompanyName;
    }


    public int getMSubCompanyId() {
        return this.mSubCompanyId;
    }


    public void setMSubCompanyId(int mSubCompanyId) {
        this.mSubCompanyId = mSubCompanyId;
    }


    public String getMDocContent() {
        return this.mDocContent;
    }


    public void setMDocContent(String mDocContent) {
        this.mDocContent = mDocContent;
    }


    public String getMDate() {
        return this.mDate;
    }


    public void setMDate(String mDate) {
        this.mDate = mDate;
    }


    public String getMTime() {
        return this.mTime;
    }


    public void setMTime(String mTime) {
        this.mTime = mTime;
    }


    public int getMAccessFileCount() {
        return this.mAccessFileCount;
    }


    public void setMAccessFileCount(int mAccessFileCount) {
        this.mAccessFileCount = mAccessFileCount;
    }


    public Long getMLocalId() {
        return this.mLocalId;
    }


    public void setMLocalId(Long mLocalId) {
        this.mLocalId = mLocalId;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean read) {
        isRead = read;
    }

    public OaInfoEntry() {
    }

    protected OaInfoEntry(Parcel in) {
        this.mRemoteId = (Long) in.readValue(Long.class.getClassLoader());
        this.mTitle = in.readString();
        this.mSubCompanyName = in.readString();
        this.mSubCompanyId = in.readInt();
        this.mDocContent = in.readString();
        this.mDate = in.readString();
        this.mTime = in.readString();
        this.mAccessFileCount = in.readInt();
    }


    @Generated(hash = 617235224)
    public OaInfoEntry(Long mLocalId, Long mRemoteId, String mTitle, String mSubCompanyName, int mSubCompanyId,
                       String mDocContent, String mDate, String mTime, int mAccessFileCount) {
        this.mLocalId = mLocalId;
        this.mRemoteId = mRemoteId;
        this.mTitle = mTitle;
        this.mSubCompanyName = mSubCompanyName;
        this.mSubCompanyId = mSubCompanyId;
        this.mDocContent = mDocContent;
        this.mDate = mDate;
        this.mTime = mTime;
        this.mAccessFileCount = mAccessFileCount;
    }

    public static final Parcelable.Creator<OaInfoEntry> CREATOR = new Parcelable.Creator<OaInfoEntry>() {
        @Override
        public OaInfoEntry createFromParcel(Parcel source) {
            return new OaInfoEntry(source);
        }

        @Override
        public OaInfoEntry[] newArray(int size) {
            return new OaInfoEntry[size];
        }
    };
}
